const goodsModel = require('../models/goods.model')
const goodsSkuModel = require('../models/goods-sku.model')

class GoodsController {
	async goodsDetailInfo(ctx, next) {
		const goodsId = ctx.request.params.id

		const result = await goodsModel.getGoodsDetailInfo(goodsId)

		ctx.body = {
			returnCode: 200,
			data: result[0],
		}
	}

	async goodsSkuAndAttrDetailInfo(ctx, next) {
		const goodsId = ctx.request.params.id

		const goodsSkuResult = await goodsModel.getGoodsSkuDetailInfo(goodsId)
		const goodsAttrResult = await goodsModel.getGoodsAttrDetailInfo(goodsId)

		ctx.body = {
			returnCode: 200,
			data: {
				goodsSkus: goodsSkuResult,
				goodsAttrKeys: goodsAttrResult,
			},
		}
	}

	async goodsSearch(ctx, next) {
		const { keyword } = ctx.request.query

		const result = await goodsModel.searchGoodsByKeyword(keyword)

		ctx.body = {
			returnCode: 200,
			data: {
				keyword,
				goodsList: result,
			},
		}
	}

	async goodsSkuStock(ctx, next) {
		const goodsId = ctx.request.params.id
		const skuId = ctx.request.params.skuId

		const [result] = await goodsSkuModel.getStockByGoodsSkuId(skuId)

		ctx.body = {
			returnCode: 200,
			data: {
				goodsId,
				skuId,
				stock: result.stock,
			},
		}
	}
}

module.exports = new GoodsController()
